from flask import Flask,render_template,redirect,request
import config
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = '1231231231'

# TODO:登录->Cookies:password=???->控制
# TODO:html->js与py交互然后需要提交password校验，以免别人控制台发送请求

@app.route('/')
def index():
    password = request.cookies.get('password')
    istrue = False
    if password is not None and password == config.getPassword():
        istrue = True
    return render_template('index.html',password = password,istrue = istrue)

@app.route('/login',methods=['GET','POST'])
def login():
    if request.method == 'GET':
        password = request.args.get('password','')
    else:
        password = request.form.get('password','')
    if password != '':
        resp = redirect('/')
        resp.set_cookie('password',password)
        return resp
    else:
        return "密码不能为空"
@app.route('/logout')
def logout():
    resp = redirect('/')
    resp.delete_cookie('password')
    return resp
@app.route('/do/<something>')
def do(something):
    password = request.cookies.get('password')
    if password is not None and password == config.getPassword():
        Flask.logger.info(f'do {something}')
        return 'done'
    else:
        return redirect('/')
if __name__ == '__main__':
    app.run(port=80)
    